package com.shiliuzi.www.service.impl;

import com.shiliuzi.www.common.Code;
import com.shiliuzi.www.pojo.bo.LimitBo;
import com.shiliuzi.www.pojo.bo.util.DownloadExcelReadBo;
import com.shiliuzi.www.pojo.entity.ExcelWriteAft;
import com.shiliuzi.www.pojo.entity.ExcelWriteBef;
import com.shiliuzi.www.pojo.vo.download.DownloadExcelCtrlVo;
import com.shiliuzi.www.pojo.vo.download.DownloadExcelJudgeVo;
import com.shiliuzi.www.pojo.vo.download.DownloadExcelVo;
import com.shiliuzi.www.service.DownloadService;
import com.shiliuzi.www.common.Result;
import com.shiliuzi.www.mapper.DownloadMapper;
import com.shiliuzi.www.pojo.dto.download.DownloadControlDto;
import com.shiliuzi.www.pojo.dto.download.DownloadExcelCheckDto;
import com.shiliuzi.www.pojo.dto.download.DownloadExcelCtrlDto;
import com.shiliuzi.www.pojo.po.RecordDefault;

import com.shiliuzi.www.util.CommonUtil;
import com.shiliuzi.www.util.LimitUtil;
import com.shiliuzi.www.util.excelUtil.ExcelReadUtil;
import com.shiliuzi.www.util.excelUtil.ExcelWriteUtil;
import com.shiliuzi.www.util.mqUtil.MQWrite;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.shiliuzi.www.common.Code.SUCCESS;
import static com.shiliuzi.www.common.Code.ERROR;
@Service
public class DownloadImpl implements DownloadService {
    @Resource
    private ExcelReadUtil excelReadUtil;

    @Resource
    private ExcelWriteUtil excelWriteUtil;

    @Resource
    private CommonUtil commonUtil;

    @Resource
    private LimitUtil limitUtil;

    @Resource
    private DownloadMapper downloadMapper;

    @Resource
    private MQWrite mqWrite;

    @Override
    public Result excelRead(MultipartFile[] excelFile) {

        if (excelFile==null) return Result.error(ERROR,"传入值为空，请查看是否填入！");

        int signal=0;
        List<String> excelNamesError = new ArrayList<>();

        for (int i = 0; i<excelFile.length;++i ){
            List<DownloadExcelReadBo> downloadExcelReadBos = excelReadUtil.judgeLegality(excelReadUtil.readRecord(excelFile[0]));

            if (downloadExcelReadBos == null) return Result.error(Code.BUE_PARAM_ERR,excelFile[i].getName()+" 请查看该文件数据是否匹配！");

            Integer successNum = downloadMapper.excelRead(downloadExcelReadBos);

            if (successNum < downloadExcelReadBos.size()) {
                signal = 1;
                excelNamesError.add(excelFile[i].getName());
            }
        }

        if (signal==1) {
            String name = "";
            for (String element : excelNamesError)
                name.concat(" "+element);

            return Result.success(SUCCESS, name+" 里部分错误数据已剔除！");
        }

        return Result.success(SUCCESS,"导入成功！");
    }

    @Override
    public Result excelCheckInfo(DownloadExcelCheckDto downloadExcelCheckDto) {

        Integer pageMax = downloadMapper.excelInfoPage(downloadExcelCheckDto.getType());

        LimitBo limitBo = limitUtil.limitAdapter(downloadExcelCheckDto, pageMax);

        List<DownloadExcelJudgeVo.DownloadExcelCheckVo> downloadExcelCheckVos
                = commonUtil.copyPropertiesList(downloadMapper.excelCheckInfo(limitBo,downloadExcelCheckDto.getPathFile()), DownloadExcelJudgeVo.DownloadExcelCheckVo.class);

        if (downloadExcelCheckVos == null || downloadExcelCheckVos.size() == 0)
            return Result.success(Code.SUCCESS,new DownloadExcelJudgeVo(null,1,0));

        return Result.success(Code.SUCCESS,new DownloadExcelJudgeVo(
                downloadExcelCheckVos
                ,commonUtil.limitCalculate(pageMax,limitBo.getPageSize(),true)
                ,limitBo.getVersion())
        );
    }

    @Override
    public Result excelWriteInfo(DownloadControlDto downloadControlDto) {

        if (!excelWriteUtil.excelWrite(downloadControlDto.getUrl(), downloadControlDto.getType(), downloadControlDto.getPathFile()))
            return Result.success(Code.SUCCESS,"数据库中暂时还有该类型数据！");

        return Result.success(Code.SUCCESS,"已经导出！请注意接收！");
    }

    //TODO 锁机制--数据更改时
    @Override
    public Result excelCtrlInfo(DownloadExcelCtrlDto downloadExcelCtrlDto) {

        Integer type = downloadExcelCtrlDto.getType();
        Integer control = downloadExcelCtrlDto.getControl();

        if (type.equals(0) && control.equals(2)) return Result.error(Code.BUE_PARAM_ERR, "不允许将错误数据导入数据库中！");

        if (control.equals(0)) return Result.success(Code.SUCCESS,
                new DownloadExcelCtrlVo(
                        downloadMapper.excelDeleteDefault(downloadExcelCtrlDto),
                        "已成功响应！删除成功！"));

        if (control.equals(1)) {
            //防止mq宕机、发送消息不进去
            commonUtil.obtainThread(()->{
                downloadExcelCtrlDto.setControl(0);
                mqWrite.sendDeadMessage(downloadExcelCtrlDto);
            });
            return Result.success(Code.SUCCESS,
                    new DownloadExcelCtrlVo(
                            0,
                            "数据保留7天后自动去除！请尽快操作！"));
        }

        commonUtil.obtainThread(()->{
            //从备份表中读取正确的备份数据
            List<ExcelWriteBef> excelWriteBefs = downloadMapper.excelPickDefault(downloadExcelCtrlDto);
            //将数据转为与数据表一致类型的对象
            List<ExcelWriteAft> excelWriteAfts = commonUtil.copyPropertiesList(excelWriteBefs, ExcelWriteAft.class);
            downloadMapper.excelWriteInfo(excelWriteAfts);
        });

        return Result.success(Code.SUCCESS,
                new DownloadExcelCtrlVo(
                        0,
                        "数据在后台进行处理！请耐心等候！") );
    }

    @Override
    public Result downloadRecord(Integer id) {
        String filePath = downloadMapper.getFilePath(id);
        if(filePath != null)return Result.success(SUCCESS,filePath);
        return Result.error(ERROR,"没有查找到内容");
    }

    @Override
    public Result getExeclInfo(Integer id,Integer identity) {
        List<RecordDefault> userInfo = downloadMapper.getUserInfo(id,identity);
//        excelWriteUtil.excelWrite(null,1,null);
        return null;
    }
}
